import {useTabsStore} from '@/store/modules/tabs'
import {Router} from "vue-router";

// 关闭tab
export const closeTab = (router: any, tab: any) => {
    const tabsStore = useTabsStore()
    if (tab.meta && tab.meta.affix) {
        return
    }

    tabsStore.delView(tab)
    toLastView(router, tabsStore.visitedViews, tab)
}

// 关闭其他tabs
export const closeOthersTabs = (router: any, tab: any) => {
    const tabsStore = useTabsStore()

    router.push(tab)
    tabsStore.delOthersViews(tab)
}

// 关闭全部tabs
export const closeAllTabs = (router: any, tab: any) => {
    const tabsStore = useTabsStore()

    tabsStore.delAllViews()
    toLastView(router, tabsStore.visitedViews, tab)
}

// 设置tab的title
export const setTabTitle = (tab: any, title: any) => {
    const tabsStore = useTabsStore()
    tabsStore.resetView(tab, {title: title})
}


// 跳转到最后一个tab
export const toLastView = (router: any, visitedViews: any[], view: any) => {
    const latestView = visitedViews.slice(-1)[0]
    if (latestView) {
        router.push(latestView.fullPath)
    } else {
        if (view.name === 'Home') {
            router.replace({path: '/redirect' + view.fullPath})
        } else {
            router.push('/')
        }
    }
}

//路由到带上tab名称
export const toTab = (router: Router, path: string, query: {}, tab_name: string) => {
    Object.assign(query, {tab_name: tab_name})
    router.push({path: path, query: query})
}

//跳转到已经打开的tab
export const toHistoryTab= (router: Router, path: string) => {
    const tabsStore = useTabsStore()
    const view:any = tabsStore.getView(path);
    if(view!=null){
        router.push({path: path, query: view.query})
    }
}
